package com.atguigu.lease.admin.controller;

import com.atguigu.lease.admin.service.FeeKeyService;
import com.atguigu.lease.admin.service.FeeValueService;
import com.atguigu.lease.model.bean.FeeKey;
import com.atguigu.lease.model.bean.FeeValue;
import com.atguigu.lease.model.result.Response;
import com.atguigu.lease.model.vo.FeeKeyVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name="杂费管理模块")
@RestController
@RequestMapping("/admin/fee")
public class FeeController {

    @Autowired
    FeeKeyService feeKeyService;
    @Autowired
    FeeValueService feeValueService;
    @Operation(summary = "5、根据id删除杂费键")
    @DeleteMapping("key/deleteById") //deleteById?feeKeyId=1
    public Response deleteFeeKeyById(@RequestParam("feeKeyId")Long id) {
        //如果杂费键下有 值不能删除
        long count = feeValueService.count(Wrappers.lambdaQuery(FeeValue.class)
                .eq(FeeValue::getFeeKeyId, id));//where fee_key_id = 1
        if(count > 0) {
            //返回一个失败的Response对象！
            throw new RuntimeException("杂费项有可用的值");
        }

        feeKeyService.removeById(id);
        return Response.ok(null);
    }
    ///admin/fee/key/saveOrUpdate
    @Operation(summary = "4、新增或更新杂费键")
    @PostMapping("key/saveOrUpdate")
    public Response saveOrUpdateKey(@RequestBody FeeKey feeKey) {
        feeKeyService.saveOrUpdate(feeKey);
        return Response.ok(null);
    }

    ///admin/fee/value/deleteById?id=1
    @Operation(summary = "3、根据id删除杂费值")
    @DeleteMapping("value/deleteById")
    public Response deleteFeeValueById(@RequestParam("id")Long id) {
        feeValueService.removeById(id);
        return Response.ok(null);
    }

    // /admin/fee/value/saveOrUpdate
    @Operation(summary = "2、新增或更新杂费值")
    @PostMapping("value/saveOrUpdate")
    public Response saveOrUpdateValue(@RequestBody FeeValue feeValue) {
        feeValueService.saveOrUpdate(feeValue);
        return Response.ok(null);
    }


    @Operation(summary = "1、查询所有的杂费键和所有值")
    @GetMapping("/list")
    public Response<List<FeeKeyVo>> list(){
//        feeKeyService.list();  只能查询feeKey的列表数据，没有查杂费值
        List<FeeKeyVo> list = feeKeyService.listFeeKeyVo();
        return Response.ok(list);
    }
}
